package com.zjr.servlet;

import java.io.IOException;
import java.util.ArrayList;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.hibernate.type.IntegerType;

import com.zjr.domain.User;
import com.zjr.service.UserService;

/**
 * Servlet implementation class UserServlet
 */
@WebServlet("/UserServlet")
public class UserServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public UserServlet() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setContentType("text/html;charset=utf-8");
		request.setCharacterEncoding("utf8");
		String path = request.getContextPath();
		String flag = request.getParameter("flag");
		User user = null;
		UserService us = null;
		
		if(flag.equals("login")){
			user = new User();
			us = new UserService();
			user.setUsername(request.getParameter("username"));
			user.setPassword(request.getParameter("password"));
			if(us.UserLogin(user) != null){
				user = us.UserLogin(user);
				HttpSession userSession = request.getSession();
				userSession.setAttribute("user", user);
				response.sendRedirect(path+"/user/user_main.jsp");
			}else{
				request.setAttribute("warm", "用户名或密码错误");
				request.getRequestDispatcher("/user/user_login.jsp")
				.forward(request, response);
			}
		}else if(flag.equals("addUser")){
			user = new User();
			us = new UserService();
			user.setUsername(request.getParameter("username"));
			user.setPassword(request.getParameter("password"));
			user.setDid(Integer.parseInt(request.getParameter("department")));
			user.setPhone(request.getParameter("phone"));
			user.setAddress(request.getParameter("address"));
			user.setPosition(Integer.parseInt(request.getParameter("position")));
			if(us.addUser(user)){
				response.sendRedirect(path+"/user/user_success.jsp");
			}else{
				response.sendRedirect(path+"/user/user_error.jsp");
			}
		}else if(flag.equals("query")){
			us = new UserService();
			ArrayList<User> users = us.queryUser();
			request.setAttribute("users", users);
			request.getRequestDispatcher("/user/user_list.jsp")
				.forward(request, response);
		}else if(flag.equals("delete")){
			us = new UserService();
			int id = Integer.parseInt(request.getParameter("id"));
			us.deleteUserById(id);
			response.sendRedirect(path+"/user/user_success.jsp");
		}else if(flag.equals("find")){
			us = new UserService();
			user = us.findUserById(Integer.parseInt(request.getParameter("id")));
			request.setAttribute("user", user);
			request.getRequestDispatcher("/user/user_modify.jsp")
				.forward(request, response);
		}else if(flag.equals("modify")){
			us = new UserService();
			user = new User();
			user.setUsername(request.getParameter("username"));
			user.setPassword(request.getParameter("password"));
			user.setDid(Integer.parseInt(request.getParameter("department")));
			user.setPhone(request.getParameter("phone"));
			user.setAddress(request.getParameter("address"));
			user.setPosition(Integer.parseInt(request.getParameter("position")));
			if(us.modifyUser(user)){
				response.sendRedirect(path+"/user/user_success.jsp");
			}else{
				response.sendRedirect(path+"/user/user_error.jsp");
			}
		}
	}

}
